<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

/**
 * 星云微讯科技有限公司
 * 后台-客户管理
 *  Client.php
 *  lichaoliang  2015-03-13
 */

class Client extends Admin_Controller
{
    private $table = 'client';
    public function __construct()
    {
        parent::__construct();
    }

    /**
     *  客户列表
     */
    public function index(){
        $list = $this->comm_model->get_list(0,100,$this->table);
        $this->load->view('admin/client/index.php',array('list'=>$list));
    }

    /**
     *  添加
     */
    public function add(){
        $this->load->view('admin/client/add.php');
    }

    /**
     *  添加逻辑
     */
    public function process_add(){
        header("Content-type:text/html;charset=utf-8");
        $view['title'] = !empty($_POST['title'])?addslashes($_POST['title']):'';
        $view['explain'] = !empty($_POST['explain'])?addslashes($_POST['explain']):0;

        $view['createtime'] = time();


        /* 图片上传 logo */
        $path  = str_replace("\\", "/", FCPATH);
        $config['upload_path'] =$path  . 'uploads/client/';
        $config['allowed_types'] = 'gif|jpg|png';
        $config['max_size'] = '2048';
        $config['max_width'] = '1024';
        $config['max_height'] = '768';
        $config['encrypt_name'] = true;
        $config['remove_spaces'] = true;

        $this->load->library('upload', $config);
        $field_name = "picture"; // 表单名称  <?php echo form_open_multipart('upload/do_upload'); <input type="file" name="field_name" size="20" />


        if ( ! $this->upload->do_upload($field_name))
        {
            $error = $this->upload->display_errors();
            echo "<script type='text/javascript'>alert('$error');window.location.href='".site_url('admin/client/add')."';</script>";
            exit;
        }
        else
        {
            $upload_data = $this->upload->data();
            $view['logo_picture'] = 'uploads/client/'.$upload_data['file_name'];

            $id = $this->comm_model->insert($this->table,$view);
            if(!empty($id)){
                echo "<script type='text/javascript'>alert('添加成功');window.location.href='".site_url('admin/client/index')."';</script>";
            }else{
                echo "<script type='text/javascript'>alert('添加失败');window.location.href='".site_url('admin/client/index')."';</script>";
            }
        }
    }

    /**
     *  修改
     */
    public function update($id){
        $view = $this->comm_model->get_one($id,$this->table);
        $this->load->view('admin/client/update.php',array('view'=>$view));
    }

    public function process_update(){
        header("Content-type:text/html;charset=utf-8");
        $id = !empty($_POST['id'])?intval($_POST['id']):0;
        $view['title'] = !empty($_POST['title'])?addslashes($_POST['title']):'';
        $view['explain'] = !empty($_POST['explain'])?addslashes($_POST['explain']):0;
        $field_name = "picture";
        if(!empty($_FILES[$field_name]['tmp_name'])) {
            /* 图片上传 logo */
            $path = str_replace("\\", "/", FCPATH);
            $config['upload_path'] = $path . 'uploads/client/';
            $config['allowed_types'] = 'gif|jpg|png';
            $config['max_size'] = '2048';
            $config['max_width'] = '1024';
            $config['max_height'] = '768';
            $config['encrypt_name'] = true;
            $config['remove_spaces'] = true;

            $this->load->library('upload', $config);

            if (!$this->upload->do_upload($field_name)) {
                $error = $this->upload->display_errors();
                echo "<script type='text/javascript'>alert('$error');window.location.href='" . site_url('admin/client/update') . "';</script>";
                exit;
            } else {
                $upload_data = $this->upload->data();
                $view['logo_picture'] = 'uploads/client/'.$upload_data['file_name'];
            }
        }

        $rs = $this->comm_model->update($id,$view,$this->table);
        if(!empty($rs)){
            echo "<script type='text/javascript'>alert('修改成功');window.location.href='".site_url('admin/client/index')."';</script>";
        }else{
            echo "<script type='text/javascript'>alert('修改失败');window.location.href='".site_url('admin/client/index')."';</script>";
        }
    }

    // 删除
    public function delete($id){
        if($id == 1){
            echo "<script type='text/javascript'>alert('星云微讯这个企业不能删除');window.location.href='".site_url('admin/client/index')."';</script>";
            exit;
        }
        $this->load->model('hot_model');
        $num = $this->hot_model->batch_update_client(array('client_id'=>$id));
        !($num===false) && $rs = $this->comm_model->del($id,$this->table);
        if(!empty($rs)){
            echo "<script type='text/javascript'>alert('删除成功，并更新" . intval($num) . "个游戏信息');window.location.href='".site_url('admin/client/index')."';</script>";
        }else{
            echo "<script type='text/javascript'>alert('删除失败');window.location.href='".site_url('admin/client/index')."';</script>";
        }
    }
}